package com.haramasu.quartz;

import com.haramasu.quartz.job.MyJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Date;

@SpringBootApplication
public class QuartzApplication implements CommandLineRunner {

	@Autowired
	private Scheduler scheduler;


	public static void main(String[] args) {
		SpringApplication.run(QuartzApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		JobDetail jobDetail= JobBuilder.newJob(MyJob.class)
				.withIdentity("diyigeJOB")
				.withDescription("to collect elk data")
				.storeDurably()
				.build();
		Trigger trigger= TriggerBuilder.newTrigger()
				.forJob(jobDetail)
				.startAt(new Date())
				.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever())
				.build();

		try {
			scheduler.scheduleJob(jobDetail, trigger);
		}catch (SchedulerException e) {
			System.out.println("任务创建失败:"+e.toString());
		}
	}
}

